#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :main.py
# @Time      :2025/1/15 13:39
# @Author    :Darcy
# @Desc      :


from PyQt6.QtWidgets import QApplication, QWidget, QTableWidgetItem, QMessageBox

from django_qt.django_tools import func_1
from table_ui import Ui_Form  # 导入 Qt Designer 生成的 UI 类


class TableDemo(QWidget, Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        # 初始化数据
        self.data = []

        # 分页相关变量
        self.current_page = 1  # 当前页码
        self.page_size = 10     # 每页显示的行数

        # 初始化表格
        self.load_data()

        # 连接按钮点击事件
        self.search_btn.clicked.connect(self.on_search_clicked)
        self.shang_btn.clicked.connect(self.on_prev_page_clicked)
        self.next_btn.clicked.connect(self.on_next_page_clicked)

    def load_data(self, keyword=None):
        """
        加载数据并显示在表格中
        :param keyword: 搜索关键字（手机号）
        """
        # 清空表格
        self.tableWidget.setRowCount(0)

        ### 此处获取数据
        filtered_data = func_1(keyword)
        self.data = filtered_data

        # 分页
        start = (self.current_page - 1) * self.page_size
        end = start + self.page_size
        paged_data = filtered_data[start:end]

        # 填充表格
        for row, item in enumerate(paged_data):
            self.tableWidget.insertRow(row)
            self.tableWidget.setItem(row, 0, QTableWidgetItem(item["name"]))
            self.tableWidget.setItem(row, 1, QTableWidgetItem(item["gender"]))
            self.tableWidget.setItem(row, 2, QTableWidgetItem(item["remark_mobiles"]))
            self.tableWidget.setItem(row, 3, QTableWidgetItem(item["corp_name"]))
            self.tableWidget.setItem(row, 4, QTableWidgetItem(item["add_time"]))

    def resizeEvent(self, event):
        """
        窗口大小变化事件
        """
        super().resizeEvent(event)

        # 动态调整列宽
        width = self.tableWidget.width()
        self.tableWidget.setColumnWidth(0, int(width * 0.2))
        self.tableWidget.setColumnWidth(1, int(width * 0.2))
        self.tableWidget.setColumnWidth(2, int(width * 0.2))
        self.tableWidget.setColumnWidth(3, int(width * 0.2))
        self.tableWidget.setColumnWidth(4, int(width * 0.2))

    def on_search_clicked(self):
        """
        搜索按钮点击事件
        """
        # 获取输入的手机号
        keyword = self.phone_inp.text().strip()

        # 重置页码为 1
        self.current_page = 1

        # 加载数据
        self.load_data(keyword)

    def on_prev_page_clicked(self):
        """
        上一页按钮点击事件
        """
        if self.current_page > 1:
            self.current_page -= 1
            self.load_data()

    def on_next_page_clicked(self):
        """
        下一页按钮点击事件
        """
        # 计算总页数
        total_pages = (len(self.data) + self.page_size - 1) // self.page_size

        if self.current_page < total_pages:
            self.current_page += 1
            self.load_data()


if __name__ == "__main__":
    app = QApplication([])
    window = TableDemo()
    window.show()
    app.exec()